package com.itcam.cm.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.itcam.cm.feign.order.OrderFeignClient;
import com.itcam.cm.model.entity.order.OrderInfo;
import com.itcam.cm.pay.service.AlipayService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    OrderFeignClient orderFeignClient;

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {

        // 测试支付宝的功能
        OrderInfo orderInfo = orderFeignClient.getOrderByOrderNo(orderNo);

        // 生成公钥和私钥
        String appId = "9021000126676072";
        // 我们的公钥
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtoQQugjZQIHFxjQZpxiPEkKdPAVWhKIXIy9LBw5z3VK3Ab7HynQgEt0NvQofBGafky4ZtyYLKEw0SGg072Ij7qQzFyFxIX0Gqjs1J09w6+pkpP0XUx7UCMJVNMfaRPRG/CASDFtBPw3OzvqgW/UnRrDiKts3KVmAi5VUhGpJm8dueYgVhxyzgE1oAKjDLEPxOQnPeJ3+4hWbqMVj+AzrvZxMLOtV6bCwnZra2IrHrdJuhZohwmVHcf/tvaxiQTzzpM7NKextE7MEyQcP6yst6ZNsDmcxFThgvsmzgR3ty0C9H/xeYfTEVLubuWZyUhY2Uc3u99LgqSCt7N/X92THXwIDAQAB";
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2hBC6CNlAgcXGNBmnGI8SQp08BVaEohcjL0sHDnPdUrcBvsfKdCAS3Q29Ch8EZp+TLhm3JgsoTDRIaDTvYiPupDMXIXEhfQaqOzUnT3Dr6mSk/RdTHtQIwlU0x9pE9Eb8IBIMW0E/Dc7O+qBb9SdGsOIq2zcpWYCLl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Rcem4MJDfM1IafxvcSH6OF0O9LYC4hc6vVteyKq/B6UBPkC4kEBjMnJbQMu5qepB2aXNKjoSaZW3ofGzxz0zd2PFCr9aorbFv8qi6ft0D/UErA/mz1qqlktkuHzdhRA9ss49gFuPs0MnhMDECgYEAsBsNrgVk5UpcDUvT71nh/thi8p37FOOnH/5iakmsaCQtHg/Lfvw+JgHKmoAZyIGiQ7R90erFqGZai5mgsxYa5GuwJzZCRkhK2raY5MMFDifjYJyHOjnPNbGBN7vFOdv6Wy/fuNiixQuVk1Lm5NbWg4ea6rCZXyezCuktt9y8JpA=";

        // 测试用户提交支付跳转支付宝页面表单接口
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do",appId,privateKey,"json","utf-8",publicKey,"RSA2");

        // 参数
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        // 请求参数
        Map<String,Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        map.put("total_amount",orderInfo.getTotalAmount());
        map.put("subject",orderInfo.getOrderItemList().get(0).getSkuName());
        map.put("product_code",orderInfo.getId());
        alipayTradeWapPayRequest.setBizContent(JSON.toJSONString(map));

        // 重要参数
        alipayTradeWapPayRequest.setReturnUrl("http://47.106.126.154/#/pages/money/paySuccess");// 同步页面跳转地址，cmurl
        alipayTradeWapPayRequest.setNotifyUrl("http://35rbvk.natappfree.cc/api/order/alipay/notify");// 异步回调地址，通知商家支付结果

        // 调用
        String form = alipayClient.pageExecute(alipayTradeWapPayRequest).getBody();
        System.out.println(form);

        // 记录支付数据，状态为未支付
        System.out.println("生成数据库payment_info表");

        return form;
    }
}
